#include "GhostObject.h"
#include <cocostudio/cocostudio.h>
Vec2 GhostObject::m_posEye = Vec2::ZERO;
Vec2 GhostObject::m_posFace = Vec2::ZERO;
Vec2 GhostObject::m_posLeftHand = Vec2::ZERO;
Vec2 GhostObject::m_posRightHand = Vec2::ZERO;
Vec2 GhostObject::m_posLids = Vec2::ZERO;
Node* GhostObject::m_nodePosGhost = nullptr;

GhostObject::GhostObject()
{
    m_body = nullptr;
    m_eyes = nullptr;
    m_face = nullptr;
    m_leftHand = nullptr;
    m_rightHand = nullptr;
    m_lid = nullptr;
}

void GhostObject::initalize(EGhostColor color,EGhostFaceState faceState,EGhostHandState handState, EGhostEyeColor eyeColor, Vec2 position, float scale,bool isAutoChangeState)
{
	setAnchorPoint(Vec2(0.5f, 0.5f));
    this->setPosition(position);
	m_scale = scale;
	m_color = color;
    m_eyeColor = eyeColor;
    
    
	setBody(m_color);
	initFace(faceState,eyeColor);
    setHand(handState);
    
    if(isAutoChangeState)
    {
        initLid();
        //setRandomFace();

        auto funcLidsClose = CallFuncN::create(CC_CALLBACK_0(GhostObject::runLidsActionClose, this));
        auto funcLidsOpen = CallFuncN::create(CC_CALLBACK_0(GhostObject::runLidsActionOpen, this));
        auto func = CallFuncN::create(CC_CALLBACK_0(GhostObject::autoChangeState,this));
        this->runAction(RepeatForever::create(Sequence::create(DelayTime::create(cocos2d::random(3.0f,6.0f)),funcLidsClose,DelayTime::create(0.1f*m_lidFrameClose.size()),func,funcLidsOpen,DelayTime::create(0.1f*m_lidFrameOpen.size()), NULL)));
    }
}
void GhostObject::initLid()
{

    
    //m_lidFrame(6);
    
    char str[50]={0};
    
    for(int i =0;i<=3;i++)
    {
        sprintf(str,"eye_eyelids_%d.png",i);
        if(m_lid == nullptr)
            m_lid = Sprite::create(str);
       // auto sprite = Sprite::create(str);
        auto frame = SpriteFrame::create(str, Rect(0,0,m_lid->getContentSize().width,m_lid->getContentSize().height));
        
        m_lidFrameClose.pushBack(frame); // Nhét vào vector
        
    }
    
    for(int i = 2; i >=0;i--)
    {
        sprintf(str,"eye_eyelids_%d.png",i);
        auto frame = SpriteFrame::create(str, Rect(0,0,m_lid->getContentSize().width,m_lid->getContentSize().height));
        
        m_lidFrameOpen.pushBack(frame); // Nhét vào vector
    }
    
    //m_lid = Sprite::createWithSpriteFrameName("bear1.png");
    //m_lid->setScale(m_scale);
    m_lid->setPosition(m_posLids);
    
    //m_lid->setVisible(false);
    //lidAction = Repeat::create(Animate::create(animation),1);

    m_body->addChild(m_lid,kGhostZOrder_Lid);

}

void GhostObject::initFace(EGhostFaceState faceState,EGhostEyeColor eyeColor)
{
    switch (faceState) {
        case kGhostFaceState_Free:
            // setRandomEyes();
            setRandomFreeFace();
            setEyes(eyeColor);
            break;
        case kGhostFaceState_Full:
            setEyes(eyeColor);
            setRandomFullFace();
            
            m_eyes->setVisible(false);
            break;
        case kGhostFaceState_None:
            setEyes(eyeColor);
            setRandomFullFace();
            
            m_eyes->setVisible(false);
            m_face->setVisible(false);
            return;
        default:
            break;
    }
	
    
    
//    m_eyes->setAnchorPoint(Vec2(0.5,0.5));
//    m_face->setAnchorPoint(Vec2(0.5,0.5));
    
//	m_eyes->setPosition(Vec2(100,0));
//	m_face->setPosition(Vec2(0,0));
	
    //setRandomFace();
    
}


//void GhostObject::setState(EGhostFaceState faceState,EGhostHandState handState)
//{
//	if (m_FaceState != faceState)
//        setFaceState(faceState);
//    if(m_HandState != handState)
//        setHandState(handState);
//	
//}
void GhostObject::setFaceState(EGhostFaceState faceState)
{
    m_FaceState = faceState;
    
//    switch (m_FaceState)
//    {
//        case kGhostFaceState_Full: runFaceStateFull(); break;
//        case kGhostFaceState_Free: runFaceStateFree(); break;
//        case kGhostFaceState_None: runFaceStateNone(); break;
//    }
}
void GhostObject::setHandState(EGhostHandState handState)
{
    m_HandState = handState;
    
//    switch (m_HandState)
//    {
//        case kGhostHandState_Angry: runHandStateAngry(); break;
//        case kGhostHandState_None:runHandStateNone(); break;
//        case kGhostHandState_Happy: runHandStateHappy(); break;
//        case kGhostHandState_Yeah: runHandStateYeah(); break;
//    }
}
EGhostFaceState GhostObject::getFaceState()
{
	return m_FaceState;
}

void GhostObject::setComponentVisible(EGhostComponent component, bool visible)
{
	switch (component)
	{
	case kGhostComponent_Body:
		m_body->setVisible(visible);
		break;
	case kGhostComponent_Eyes:
		m_eyes->setVisible(visible);
		break;
	case kGhostComponent_Face:
		m_face->setVisible(visible);
		break;
	case kGhostComponent_LeftHand:
		m_leftHand->setVisible(visible);
		break;
	case kGhostComponent_RightHand:
		m_rightHand->setVisible(visible);
		break;
	}
}

void GhostObject::autoChangeState()
{
    EGhostFaceState faceState = getRandomFaceState();
    setFace(faceState,EGhostStatus::kGhostStatus_Random);
    if(faceState == EGhostFaceState::kGhostFaceState_Free)
        runEyesAction();
}
void GhostObject::changeGhostState(EGhostStatus ghostStatus)
{
    m_ghostStatus = ghostStatus;
    switch (ghostStatus) {
        case kGhostStatus_Happy:
            
            break;
        case kGhostStatus_Angry:
            
            break;
        case kGhostStatus_Lol:
            
            break;
        case kGhostStatus_Sad:
            
            break;
        case kGhostStatus_Yeah:
            runStateYeah();
            break;
        case kGhostStatus_Lose_Easy:
            runStateLoseEasy();
            break;
        case kGhostStatus_Lose_Normal:
            runStateLoseNormal();
            break;
        case kGhostStatus_Lose_Hard:
            runStateLoseHard();
            break;
        case kGhostStatus_Win_Easy:
            runStateWinEasy();
            break;
        case kGhostStatus_Win_Normal:
            runStateWinNormal();
            break;
        case kGhostStatus_Win_Hard:
            runStateWinHard();
            break;
        default:
            break;
    }
}
void GhostObject::runStateYeah()
{
    setFaceState(EGhostFaceState::kGhostFaceState_Free);
    setFace(EGhostFaceState::kGhostFaceState_Free, EGhostStatus::kGhostStatus_Lol);
    
    setHand(EGhostHandState::kGhostHandState_Yeah);
    
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
    this->m_leftHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.1f, 30),RotateBy::create(0.2f,-30), NULL)));
    this->m_rightHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.1f, -30),RotateBy::create(0.2f,30), NULL)));
}

void GhostObject::runStateLoseEasy()
{
    
    setFaceState(EGhostFaceState::kGhostFaceState_Full);
    setFace(EGhostFaceState::kGhostFaceState_Full, EGhostStatus::kGhostStatus_Lose_Easy);
    setHand(EGhostHandState::kGhostHandState_Yeah);
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
}
void GhostObject::runStateLoseNormal()
{
    
    setFaceState(EGhostFaceState::kGhostFaceState_Full);
    setFace(EGhostFaceState::kGhostFaceState_Full, EGhostStatus::kGhostStatus_Lose_Normal);
    setHand(EGhostHandState::kGhostHandState_Yeah);
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
}
void GhostObject::runStateLoseHard()
{
    
    setFaceState(EGhostFaceState::kGhostFaceState_Full);
    setFace(EGhostFaceState::kGhostFaceState_Full, EGhostStatus::kGhostStatus_Lose_Hard);
    setHand(EGhostHandState::kGhostHandState_Yeah);
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
}
void GhostObject::runStateWinEasy()
{
    
    setFaceState(EGhostFaceState::kGhostFaceState_Full);
    setFace(EGhostFaceState::kGhostFaceState_Full, EGhostStatus::kGhostStatus_Win_Easy);
    setHand(EGhostHandState::kGhostHandState_Yeah);
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
}
void GhostObject::runStateWinNormal()
{
    
    setFaceState(EGhostFaceState::kGhostFaceState_Full);
    setFace(EGhostFaceState::kGhostFaceState_Full, EGhostStatus::kGhostStatus_Win_Normal);
    setHand(EGhostHandState::kGhostHandState_Yeah);
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
}
void GhostObject::runStateWinHard()
{
    
    setFaceState(EGhostFaceState::kGhostFaceState_Full);
    setFace(EGhostFaceState::kGhostFaceState_Full, EGhostStatus::kGhostStatus_Win_Hard);
    setHand(EGhostHandState::kGhostHandState_Yeah);
    this->m_leftHand->setVisible(true);
    this->m_rightHand->setVisible(true);
    this->m_leftHand->setAnchorPoint(Vec2(1.0f,0.0f));
    this->m_rightHand->setAnchorPoint(Vec2(0.0f,0.0f));
}




void GhostObject::setFace(EGhostFaceState faceState, EGhostStatus status)
{
    std::string face = "";
	m_ghostStatus = status;
    if(faceState == kGhostFaceState_Free)
    {
            switch (status) {
                case kGhostStatus_Happy:
                    face = cocos2d::String::createWithFormat("face_happy.png")->getCString();
                    break;
                case kGhostStatus_Angry:
                    face = cocos2d::String::createWithFormat("face_sad.png")->getCString();
                    break;
                case kGhostStatus_Lol:
                    face = cocos2d::String::createWithFormat("face_lol.png")->getCString();
                    break;
                case kGhostStatus_Sad:
                    face = cocos2d::String::createWithFormat("face_sad.png")->getCString();
                    break;
                case kGhostStatus_Yeah:
                    face = cocos2d::String::createWithFormat("happy_2.png")->getCString();
                    break;
                case kGhostStatus_Idle:
                    face = cocos2d::String::createWithFormat("idle_%d.png",cocos2d::random(1,kNumberIdleState))->getCString();
                    break;
                case kGhostStatus_Random:
                    setRandomFreeFace();
                    break;
                default:
                    return;
                    
            }
        setEyes(m_eyeColor);
        
    }
    else if(faceState  ==  kGhostFaceState_Full)
    {
            switch (status) {
                case kGhostStatus_Happy:
                    face = cocos2d::String::createWithFormat("face_1.png")->getCString();
                    break;
                case kGhostStatus_Angry:
                    face = cocos2d::String::createWithFormat("face_7.png")->getCString();
                    break;
                case kGhostStatus_Lol:
                    face = cocos2d::String::createWithFormat("win_hard.png")->getCString();
                    break;
                case kGhostStatus_Sad:
                    face = cocos2d::String::createWithFormat("face_6.png")->getCString();
                    break;
                case kGhostStatus_Yeah:
                    face = cocos2d::String::createWithFormat("win_normal.png")->getCString();
                    break;
                case kGhostStatus_Lose_Easy:
                    face = cocos2d::String::createWithFormat("lose_easy.png")->getCString();
                    break;
                case kGhostStatus_Lose_Normal:
                    face = cocos2d::String::createWithFormat("lose_normal.png")->getCString();
                    break;
                case kGhostStatus_Lose_Hard:
                    face = cocos2d::String::createWithFormat("lose_hard.png")->getCString();
                    break;
                case kGhostStatus_Win_Easy:
                    face = cocos2d::String::createWithFormat("win_easy.png")->getCString();
                    break;
                case kGhostStatus_Win_Normal:
                    face = cocos2d::String::createWithFormat("win_normal.png")->getCString();
                    break;
                case kGhostStatus_Win_Hard:
                    face = cocos2d::String::createWithFormat("win_hard.png")->getCString();
                    break;
                case kGhostStatus_Idle:
                    face = cocos2d::String::createWithFormat("face_2.png")->getCString();
                    break;
                case kGhostStatus_Random:
                    face = cocos2d::String::createWithFormat("face_%d.png",cocos2d::random(1,kNumberFullFaceState))->getCString();
                    break;
                default:
                    break;

            }
        m_eyes->setVisible(false);
    }
    
    if(face != "")
    {
        if(m_face == nullptr)
        {
            m_face = Sprite::create(face);
            m_face->setPosition(m_body->getContentSize().width/2,m_body->getContentSize().height/2);
            m_face->setScale(m_scale);
            m_body->addChild(m_face,kGhostZOrder_Face);
        }
        else
            m_face->setTexture(TextureCache::sharedTextureCache()->addImage((face)));
    
        m_face->setVisible(true);
    }
}


EGhostFaceState GhostObject::getRandomFaceState()
{
    switch (cocos2d::random(1, kNumberFaceState)) {
        case 1:
            return EGhostFaceState::kGhostFaceState_Free;
        case 2:
            return EGhostFaceState::kGhostFaceState_Full;
        default:
            return EGhostFaceState::kGhostFaceState_None;
    }
}
EGhostStatus GhostObject::getRandomStatus()
{
    switch (cocos2d::random(1,kNumberStatus)) {
        case 1: return EGhostStatus::kGhostStatus_Yeah;
        case 2: return EGhostStatus::kGhostStatus_Angry;
        case 3: return EGhostStatus::kGhostStatus_Happy;
        case 4: return EGhostStatus::kGhostStatus_Idle;
        case 5: return EGhostStatus::kGhostStatus_Lol;
        case 6: return EGhostStatus::kGhostStatus_Sad;
        
        
        default:
            break;
    }
}


EGhostHandState GhostObject::getRandomHandState()
{
    switch (cocos2d::random(1, kNumberHandState)) {
        case 1:
            return EGhostHandState::kGhostHandState_Yeah;
        case 2:
            return EGhostHandState::kGhostHandState_None;
        case 3:
            return EGhostHandState::kGhostHandState_Happy;
        case 4:
            return EGhostHandState::kGhostHandState_Angry;
        default:
            return EGhostHandState::kGhostHandState_Angry;
    }
}



void GhostObject::setRandomEyes()
{
    std::string eyes;
    switch (cocos2d::random(1, kNumberEyes)) {
        case 1: eyes = "eye_blue.png"; break;
        case 2: eyes = "eye_cyan.png"; break;
        case 3: eyes = "eye_green.png"; break;
        case 4: eyes = "eye_orange.png"; break;
        case 5: eyes = "eye_purple.png"; break;
        default: eyes = nullptr;
    }
    if(m_eyes == nullptr)
    {
        m_eyes = Sprite::create(eyes);
        //m_eyes->setPosition(m_body->getContentSize().width/2,m_body->getContentSize().height/2);
        //m_eyes->setScale(m_scale);
        
        m_body->addChild(m_eyes, EGhostZOrder::kGhostZOrder_Eyes);
    }
    else
        m_eyes->setTexture(TextureCache::sharedTextureCache()->addImage((eyes)));
    m_eyes->stopAllActions();
    m_eyes->setPosition(GhostObject::m_posEye);
    
    m_eyes->setVisible(true);
}
void GhostObject::setBody(EGhostColor ghostColor)
{
    std::string body = "";
    switch (ghostColor) {
        case kGhostColor_Black: body = "ghost_black.png"; break;
        case kGhostColor_Blue: body = "ghost_blue.png"; break;
        case kGhostColor_Cyan: body = "ghost_cyan.png"; break;
        case kGhostColor_Green: body = "ghost_green.png"; break;
        case kGhostColor_Magenta: body = "ghost_magenta.png"; break;
        case kGhostColor_Orange: body = "ghost_orange.png"; break;
        case kGhostColor_Pink: body = "ghost_pink.png"; break;
        case kGhostColor_Purple: body = "ghost_purple.png"; break;
        case kGhostColor_Red: body = "ghost_red.png"; break;
        case kGhostColor_Yellow: body = "ghost_yellow.png"; break;
        case kGhostColor_Easy_NonStar: body = "ghost_easy_base.png"; break;
        case kGhostColor_Easy_0Star: body = "ghost_easy_0star.png"; break;
        case kGhostColor_Easy_1Star: body = "ghost_easy_1star.png"; break;
        case kGhostColor_Easy_2Star: body = "ghost_easy_2star.png"; break;
        case kGhostColor_Easy_3Star: body = "ghost_easy_3star.png"; break;
        case kGhostColor_Normal_NonStar: body = "ghost_normal_base.png"; break;
        case kGhostColor_Normal_0Star: body = "ghost_normal_0star.png"; break;
        case kGhostColor_Normal_1Star: body = "ghost_normal_1star.png"; break;
        case kGhostColor_Normal_2Star: body = "ghost_normal_2star.png"; break;
        case kGhostColor_Normal_3Star: body = "ghost_normal_3star.png"; break;
        case kGhostColor_Hard_NoneStar: body = "ghost_hard_base.png"; break;
        case kGhostColor_Hard_0Star: body = "ghost_hard_0star.png"; break;
        case kGhostColor_Hard_1Star: body = "ghost_hard_1star.png"; break;
        case kGhostColor_Hard_2Star: body = "ghost_hard_2star.png"; break;
        case kGhostColor_Hard_3Star: body = "ghost_hard_3star.png"; break;
        default: break;
    }
    if(body == "")
    {
        setRandomBody();
    }
    else
    {
    if(m_body == nullptr)
    {
        m_body = Button::create(body,body,body);
        m_body->setScale(m_scale);
        this->addChild(m_body, EGhostZOrder::kGhostZOrder_Body);
    }
    else
        m_body->loadTextures(body, body);
    }
}
void GhostObject::setHand(EGhostHandState handState)
{
    std::string hand;
    switch (handState) {
        case kGhostHandState_Angry: hand = "lose_arm_hard"; break;
        case kGhostHandState_Happy: hand = "win_arm_normal"; break;
        case kGhostHandState_Yeah: hand = "lose_arm_normal"; break;
        case kGhostHandState_None: hand = ""; break;
        default: hand = nullptr;
    }
    
    
    if(m_leftHand == nullptr || m_rightHand == nullptr)
    {
        if(hand != "")
        {
            std::string str = cocos2d::String::createWithFormat("%s_%s.png",hand.c_str(),"left")->getCString();
            m_leftHand = Sprite::create("lose_arm_normal_left.png");
            m_rightHand = Sprite::create(cocos2d::String::createWithFormat("%s_%s.png",hand.c_str(),"right")->getCString());
            m_leftHand->setScale(m_scale);
            m_rightHand->setScale(m_scale);
            
            //CCLOG("%f,%f",
            
            m_leftHand->setPosition(Vec2(m_posLeftHand.x * m_scale,m_posLeftHand.y *m_scale));
            m_rightHand->setPosition(Vec2(m_posRightHand.x * m_scale,m_posLeftHand.y*m_scale));
            
            CCLOG("%f,%f",m_leftHand->getPosition().x,m_leftHand->getPosition().y);
             CCLOG("%f,%f",m_rightHand->getPosition().x,m_rightHand->getPosition().y);
            this->addChild(m_leftHand, EGhostZOrder::kGhostZOrder_Hand);
            this->addChild(m_rightHand, EGhostZOrder::kGhostZOrder_Hand);
        }
    }
    else
    {
        if(hand == "")
        {
            m_leftHand->setVisible(false);
            m_rightHand->setVisible(false);
        }
        else
        {
            m_leftHand->setTexture(TextureCache::sharedTextureCache()->addImage(cocos2d::String::createWithFormat("%s_%s.png",hand.c_str(),"left")->getCString()));
            m_rightHand->setTexture(TextureCache::sharedTextureCache()->addImage(cocos2d::String::createWithFormat("%s_%s.png",hand.c_str(),"right")->getCString()));
//            m_leftHand->setScale(m_scale);
//            m_rightHand->setScale(m_scale);
            
            //m_leftHand->setPosition(Vec2(m_posLeftHand.x * m_scale,m_posLeftHand.y *m_scale));
            //m_rightHand->setPosition(Vec2(m_posRightHand.x * m_scale,m_posLeftHand.y*m_scale));
            m_leftHand->setVisible(true);
            m_rightHand->setVisible(true);
        }
    }
    
}

void GhostObject::setEyes(EGhostEyeColor eyeColor)
{
    std::string eye ="";
    switch (eyeColor) {
        case kGhostEyeColor_Blue: eye= "eye_blue.png"; break;
        case kGhostEyeColor_Cyan: eye= "eye_cyan.png"; break;
        case kGhostEyeColor_Green: eye= "eye_green.png"; break;
        case kGhostEyeColor_Orange: eye= "eye_orange.png"; break;
        case kGhostEyeColor_Purple: eye= "eye_purple.png"; break;
        default: break;
    }
    if(eye == "")
        setRandomEyes();
    else
    {
    if(m_eyes == nullptr)
    {
        m_eyes = Sprite::create(eye);
        //m_eyes->setPosition(m_body->getContentSize().width/2,m_body->getContentSize().height/2);
        //m_eyes->setScale(m_scale);
        
        m_body->addChild(m_eyes, EGhostZOrder::kGhostZOrder_Eyes);
    }
    else
        m_eyes->setTexture(TextureCache::sharedTextureCache()->addImage((eye)));
    m_eyes->stopAllActions();
    m_eyes->setPosition(GhostObject::m_posEye);
    
    m_eyes->setVisible(true);
    }
}
void GhostObject::setRandomBody()
{
    std::string body = "";
    switch (cocos2d::random(1,kNumberBody)) {
        case 1: body = "ghost_black.png"; break;
        case 2: body = "ghost_blue.png"; break;
        case 3: body = "ghost_cyan.png"; break;
        case 4: body = "ghost_green.png"; break;
        case 5: body = "ghost_magenta.png"; break;
        case 6: body = "ghost_orange.png"; break;
        case 7: body = "ghost_pink.png"; break;
        case 8: body = "ghost_purple.png"; break;
        case 9: body = "ghost_red.png"; break;
        case 10: body = "ghost_yellow.png"; break;
            
        default: body = "ghost_black.png"; break;
    }
   
        if(m_body == nullptr)
        {
            m_body = Button::create(body,body,body);
            m_body->setScale(m_scale);
            this->addChild(m_body, EGhostZOrder::kGhostZOrder_Body);
        }
        else
            m_body->loadTextures(body, body);

}

void GhostObject::setRandomFace()
{
    switch (cocos2d::random(1, kNumberFaceState)) {
        case 1:
            setRandomFreeFace();
            break;
        case 2:
            setRandomFullFace();
        default:
            
            break;
    }
}
void GhostObject::setRandomFreeFace()
{
    std::string face;
    switch (cocos2d::random(1, kNumberFreeFaceState)) {
        case 1: face = cocos2d::String::createWithFormat("%s%d.png","happy_",cocos2d::random(1, kNumberHappyState))->getCString(); break;
        case 2: face = cocos2d::String::createWithFormat("%s%d.png","idle_",cocos2d::random(1, kNumberIdleState))->getCString(); break;

        default: face = nullptr;
    }
    if(m_face == nullptr)
    {
        m_face = Sprite::create(face);
        
        //m_face->setScale(m_scale);
        m_face->setPosition(GhostObject::m_posFace);
        m_body->addChild(m_face, EGhostZOrder::kGhostZOrder_Face);
    }
    else
        m_face->setTexture(TextureCache::sharedTextureCache()->addImage(face));
    m_face->setVisible(true);
    
    //setRandomEyes();

}

void GhostObject::setRandomFullFace()
{
    m_eyes->setVisible(false);
    m_eyes->stopAllActions();
    std::string face = cocos2d::String::createWithFormat("%s%d.png","face_",cocos2d::random(1, kNumberFullFaceState))->getCString();
    if(m_face == nullptr)
    {
        m_face = Sprite::create(face);
        m_face->setPosition(m_body->getContentSize().width/2,m_body->getContentSize().height/2);
       // m_face->setScale(m_scale);
        m_face->setPosition(GhostObject::m_posFace);
        m_body->addChild(m_face, EGhostZOrder::kGhostZOrder_Face);
    }
    else
        m_face->setTexture(TextureCache::sharedTextureCache()->addImage(face));
    m_face->setVisible(true);
}
//void GhostObject::runFaceStateFree()
//{}

void GhostObject::runEyesAction()
{
    float time = (float)cocos2d::random(1, (int)kTimeChangeState);
    auto delayTime =DelayTime::create(time);
    
    int range = m_lid->getContentSize().width*m_scale/2 - m_eyes->getContentSize().width*m_scale*0.6f;
    float newPointX = cocos2d::random(-range,range);
    float newPointY = cocos2d::random(0,1) == 0?-(newPointX + range): newPointX + range;
    
    auto moveTo = MoveTo::create(0.5f, Vec2(m_eyes->getPosition().x + newPointX, m_eyes->getPosition().y + newPointY));
    auto action = RepeatForever::create(Sequence::create(delayTime,moveTo, NULL));
    m_eyes->runAction(action);
}
void GhostObject::runLidsActionClose()
{
    //m_lid->setVisible(true);
    auto m_lidAnimation = Animation::createWithSpriteFrames(m_lidFrameClose,0.1f);
    m_lid->runAction(Animate::create(m_lidAnimation));
}
void GhostObject::runLidsActionOpen()
{
    auto m_lidAnimation = Animation::createWithSpriteFrames(m_lidFrameOpen,0.1f);
    m_lid->runAction(Animate::create(m_lidAnimation));
}


void GhostObject::ReadPositionComponent()
{
    if(GhostObject::m_nodePosGhost == nullptr)
        GhostObject::m_nodePosGhost = CSLoader::getInstance()->createNode("SceneTemp.csb")->getChildByName("GhostTemp");
    
    GhostObject::m_posLeftHand = m_nodePosGhost->getChildByName("LeftHand")->getPosition();
    GhostObject::m_posRightHand = m_nodePosGhost->getChildByName("RightHand")->getPosition();
    
    GhostObject::m_posFace = m_nodePosGhost->getChildByName("Body")->getChildByName("Face")->getPosition();
   
    GhostObject::m_posEye = m_nodePosGhost->getChildByName("Body")->getChildByName("Eye")->getPosition();
    
    GhostObject::m_posLids = m_nodePosGhost->getChildByName("Body")->getChildByName("Lid")->getPosition();
    
    GhostObject::m_nodePosGhost->retain();
}
void GhostObject::ChangePositionChomponent(float scale)
{
    if(GhostObject::m_nodePosGhost == nullptr)
        ReadPositionComponent();


    GhostObject::m_nodePosGhost->setScale(scale);
    GhostObject::m_posLeftHand = m_nodePosGhost->getChildByName("LeftHand")->getPosition();
    GhostObject::m_posRightHand = m_nodePosGhost->getChildByName("RightHand")->getPosition();
    
    GhostObject::m_posFace = m_nodePosGhost->getChildByName("Body")->getChildByName("Face")->getPosition();
    
    GhostObject::m_posEye = m_nodePosGhost->getChildByName("Body")->getChildByName("Eye")->getPosition();
    GhostObject::m_posLids = m_nodePosGhost->getChildByName("Body")->getChildByName("Lid")->getPosition();
}

EGhostColor GhostObject::GetRandomBody()
{
   
    switch (cocos2d::random(1,kNumberBody)) {
        case 1: return EGhostColor::kGhostColor_Black;
        case 2: return EGhostColor::kGhostColor_Blue;
        case 3: return EGhostColor::kGhostColor_Cyan;
        case 4: return EGhostColor::kGhostColor_Green;
        case 5: return EGhostColor::kGhostColor_Magenta;
        case 6: return EGhostColor::kGhostColor_Orange;
        case 7: return EGhostColor::kGhostColor_Pink;
        case 8: return EGhostColor::kGhostColor_Purple;
        case 9: return EGhostColor::kGhostColor_Red;
        case 10: return EGhostColor::kGhostColor_Yellow;
            
        default: return EGhostColor::kGhostColor_Blue;
    }

}

void GhostObject::addComponent(cocos2d::Node *component, int  layer)
{
    component->setPosition(m_body->getContentSize().width/2
                           ,-m_body->getContentSize().height/4*m_scale);
    this->m_body->addChild(component,layer);
}
